# import json
# import logging
# import uuid as uuid_module
# import time
# import re
# import emoji
# from datetime import datetime
# import uuid as uuid
#
# from django.views import View
# from django.http.request import HttpRequest
# from django.http import JsonResponse
# from django.conf import settings
# from django.db import models
# from django.utils import timezone
# from django.db.models import Q
#
# from .models import MediaModel as MediaModel_Class
# from .utils import idmi_nlp_tool
#
# MediaModel = MediaModel_Class()
#
# def generate_unique_id():
#     unique_id = uuid.uuid4().hex
#     return unique_id
#
#
#
# class StoreMedia(View):
#     '''
#         store the cuzu sent
#     {
#         "id": 67369,
#         "sourceId": "dongchedi_public_praise",
#         "url_temp": "7087644106989506592",
#         "price": "8.60万",
#         "boughtdate": "2018-12-01",
#         "address": "茂名",
#         "create_time": "2018-12-01",
#         "image_url": "",
#         "brand": "Toyota",
#         "series": "Corolla ",
#         "carspec_name": "1.8L CVT GLX-i",
#         "title": "",
#         "customer_voice": "人生第一部车 ，不算很完美 ，也没很差劲，优点是:毛病少省油动力还过得去。缺点是:对我而言空间小了些，车漆非常薄，指甲不小心碰下都有微妙的划痕",
#         "URL": "https://www.dongchedi.com/koubei/7087644106989506592",
#     }
#     '''
#
#     def post(self, request: HttpRequest) -> JsonResponse:
#         MediaModel.sourceId = request.POST.get('sourceId')
#         MediaModel.uuid = str(uuid_module.uuid4()).replace('-', '')
#         MediaModel.url_temp = request.POST.get('url_temp')
#         MediaModel.create_time = str(datetime.now())[:10]
#         MediaModel.image_url = request.POST.get('image_url')
#         MediaModel.brand = request.POST.get('brand')
#         MediaModel.series = request.POST.get('series')
#         MediaModel.carspec_name = request.POST.get('carspec_name')
#         __title = request.POST.get('title')
#         __text = request.POST.get('text')
#         MediaModel.title = __title
#         MediaModel.text = __text
#
#         title = self.clean_sentence(__title)
#         text = self.clean_sentence(__text)[:2000]
#
#         # 对数据进行翻译
#         if len(title):
#             title_en = idmi_nlp_tool._translate_util_zh2en(sentence=title)['en']
#         else:
#             title_en = ''
#         if len(text):
#             text_en = idmi_nlp_tool._translate_util_zh2en(sentence=text)['en']
#         else:
#             text_en = ''
#         MediaModel.text_en = text_en
#         MediaModel.title_en = title_en
#
#
#         if len(text):
#             zh, en = idmi_nlp_tool.qwen.csp(customer_voice=text)
#             if len(zh):
#                 MediaModel.llm_csp = json.dumps(zh, ensure_ascii=False)
#                 MediaModel.llm_csp_en = json.dumps(en, ensure_ascii=False)
#             else:
#                 MediaModel.llm_csp = ''
#                 MediaModel.llm_csp_en = ''
#             zh, en = idmi_nlp_tool.qwen.feel(customer_voice=text)
#             if len(zh):
#                 MediaModel.llm_feeling = json.dumps(zh, ensure_ascii=False)
#                 MediaModel.llm_feeling_en = json.dumps(en, ensure_ascii=False)
#             else:
#                 MediaModel.llm_feeling = ''
#                 MediaModel.llm_feeling_en = ''
#         else:
#             MediaModel.llm_csp = ''
#             MediaModel.llm_csp_en = ''
#             MediaModel.llm_feeling = ''
#             MediaModel.llm_feeling_en = ''
#
#         MediaModel.URL = request.POST.get('URL')
#         MediaModel.save()
#         return JsonResponse({'code': 200, 'msg': 'ok'})
#
#
#     def clean_sentence(self, sentence):
#         if len(sentence):
#             sentence = sentence. \
#                 replace('\r', ''). \
#                 replace('\n', ''). \
#                 replace('\t', ''). \
#                 replace('"', ''). \
#                 replace("'", ""). \
#                 replace('  ', ""). \
#                 replace('“', ' '). \
#                 replace('”', ' ')
#             try:
#                 sentence = emoji.demojize(sentence)
#             except:
#                 sentence = sentence
#             return sentence
#         else:
#             return ''
#
# import codecs
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# #
# '''
#
# with codecs.open(filename='/root/03_backend/idmi-backend-v5/idmi_backend/sales-zh2en.json',
#                  mode='r', encoding='utf-8') as fr:
#     zh2en_dict = json.load(fr)
#
#
# class UpdateMedia(View):
#     #
#     # Update Media in MySQL Database
#     #
#     def post(self, request):
#         line = request.POST.get('line')
#         line = json.loads(line)
#         #         ['id', 'sourceId', 'url_temp', 'create_time', 'image_url',
#         # 'brand', 'series', 'carspec_name', 'title', 'text', 'URL',
#         #  'title_en', 'text_en', 'nickname', 'read_times',
#         #  'llm_quality', 'llm_csp', 'llm_feeling', 'llm_quality_en',
#         #  'llm_csp_en', 'llm_feeling_en']
#
#         sourceId = line['sourceId']
#         url_temp = line['url_temp']
#         image_url = line['image_url']
#         brand = line['brand']
#         series = line['series']
#         carspec_name = line['carspec_name']
#         title = line['title']
#         text = line['text']
#         URL = line['URL']
#         title_en = line['title_en']
#         text_en = line['text_en']
#         nickname = line['nickname']
#         read_times = line['read_times']
#
#         llm_quality = line['llm_quality']
#         llm_csp = line['llm_csp']
#         llm_feeling = line['llm_feeling']
#         llm_quality_en = line['llm_quality_en']
#         llm_csp_en = line['llm_csp_en']
#         llm_feeling_en = line['llm_feeling_en']
#
#         llm_csp_convert = []
#         llm_feeling_convert = []
#         llm_csp_en_convert = []
#         llm_feeling_en_convert = []
#
#         if isinstance(llm_feeling, str):
#             if len(llm_feeling) > 0:
#                 llm_feeling = json.loads(llm_feeling[1:-1])
#                 for feeling in llm_feeling:
#                     feel_1 = feeling['feel_1']
#                     feel_2 = feeling['feel_2']
#                     sentiment = feeling['sentiment']
#                     content_list = feeling['content_list']
#                     feel_1_en = zh2en_dict[feel_1]
#                     feel_2_en = zh2en_dict[feel_2]
#                     if sentiment == '正向情感':
#                         sentiment_en = 'positive'
#                     elif sentiment == '负向情感':
#                         sentiment_en = 'negative'
#                     elif sentiment == '中性情感':
#                         sentiment_en = 'neutral'
#                     else:
#                         raise RuntimeError('sentiment error')
#
#                     llm_feeling_convert.append({
#                         'feel_1': feel_1,
#                         'feel_2': feel_2,
#                         'content_list': content_list,
#                         'sentiment': sentiment,
#                     })
#                     llm_feeling_en_convert.append({
#                         'feel_1': feel_1_en,
#                         'feel_2': feel_2_en,
#                         'content_list': content_list,
#                         'sentiment': sentiment_en,
#                     })
#         if isinstance(llm_csp, str):
#             if len(llm_csp) > 0:
#                 llm_csp = json.loads(llm_csp[1:-1])
#                 for csp in llm_csp:
#                     csp_1 = csp['csp_1']
#                     csp_2 = csp['csp_2']
#                     csp_3 = csp['csp_3']
#                     sentiment = csp['sentiment']
#                     content_list = csp['content_list']
#                     csp_1_en = zh2en_dict[csp_1]
#                     csp_2_en = zh2en_dict[csp_2]
#                     csp_3_en = zh2en_dict[csp_3]
#                     if sentiment == '正向情感':
#                         sentiment_en = 'positive'
#                     elif sentiment == '负向情感':
#                         sentiment_en = 'negative'
#                     elif sentiment == '中性情感':
#                         sentiment_en = 'neutral'
#                     else:
#                         raise RuntimeError('sentiment error')
#
#                     llm_csp_convert.append({
#                         'csp_1': csp_1,
#                         'csp_2': csp_2,
#                         'csp_3': csp_3,
#                         'content_list': content_list,
#                         'sentiment': sentiment,
#                     })
#                     llm_csp_en_convert.append({
#                         'csp_1': csp_1_en,
#                         'csp_2': csp_2_en,
#                         'csp_3': csp_3_en,
#                         'content_list': content_list,
#                         'sentiment': sentiment_en,
#                     })
#         MediaModel.uuid = generate_unique_id()
#         MediaModel.sourceId = sourceId
#         MediaModel.create_time = timezone.now()
#         MediaModel.update_time = timezone.now()
#         MediaModel.image_url = image_url
#         MediaModel.brand = brand
#         MediaModel.series = series
#         MediaModel.carspec_name = carspec_name
#         MediaModel.title = title
#         MediaModel.text = text
#         MediaModel.URL = URL
#         MediaModel.title_en = title_en
#         MediaModel.text_en = text_en
#         MediaModel.nickname = nickname
#         MediaModel.read_times = read_times
#         MediaModel.llm_quality = llm_quality
#         MediaModel.llm_csp = json.dumps(llm_csp_convert, ensure_ascii=False)
#         MediaModel.llm_feeling = json.dumps(llm_feeling_convert, ensure_ascii=False)
#         MediaModel.llm_quality_en = llm_quality_en
#         MediaModel.llm_csp_en = json.dumps(llm_csp_en_convert, ensure_ascii=False)
#         MediaModel.llm_feeling_en = json.dumps(llm_feeling_en_convert, ensure_ascii=False)
#         MediaModel.save(force_insert=True)
#
#         return JsonResponse({'status': 200})
#
#
# '''
#
#
#
#
#
#
#
#
